/**************************************************************************
 *				SRLOS TEAM ALL RIGHT RESERVED
 *@filename head.c
 *@autor	bloceanc
 *@date		08/09/2010
 *@note		this file is the head of kernel after assember file using C.
 *			in this file we initialize all modules, and create system 
 *			default process!
 **************************************************************************/

extern long long ll_tick_count;



extern void mem_physical_page_mgr_init(unsigned int dram_start_addr);
extern void mem_slab_initialize(void);
extern void mt_initialize(void);
extern void task_module_init(void);
extern void comm_global_critical_section_init(void);
extern void timer_initialize(void);
extern void	int_initialize_table(void);
extern void hw_timer_initialize(void);
extern void head_create_default_task(unsigned int *code_addr);
extern void drv_led_initialize(void);
extern void drv_led_on(int lednum);
extern void drv_led_off(int lednum);

#include "commdef.h"
#include "os_config.h"
#include "task.h"


/*******************************************************
 * 		JUST USED FOR SIMPLE TESTING
 *******************************************************/
extern void comm_global_critical_section_enter(void);
extern void comm_global_critical_section_leave(void);

/*
 * default process thread function
 */
void kernel_main_default(void)
{
	drv_led_initialize();
	hw_timer_initialize();
	
	// we can do any test at here after system started.^_^
	if(task_process_fork())
	{
		unsigned int x = 0;
		unsigned int flag = 0;
		// parent process, the return value is child process ID
		// make led 2 on
		// start timer, must do this here!(in default process)
		drv_led_on(2);
		while(1)
		{
			x++;
			if(x == 0xffffffff)
				x = 0;
			
			if(!(x & 0xffff))
			{
				comm_global_critical_section_enter();
				if(!flag)
					drv_led_off(2);
				else
					drv_led_on(2);
				comm_global_critical_section_leave();
				flag = !flag;
			}
		}
		
		// 如果不是死循环，执行完这里之后会跳转到压入栈中的L14的位置，L14我们再初始化默认系统进程的线程是，让他返回SWI中断的后半部分
		// 从而实现返回到用户模式开始执行，详细的过程请绘制系统态的栈图来理解^_^
	}
	else
	{
		// this is child process
		unsigned int x = 0;
		unsigned int flag = 0;
		drv_led_on(1);
		while(1)
		{
			x++;
			if(x == 0xffffffff)
				x = 0;
			if(!(x & 0xffff))
			{
				comm_global_critical_section_enter();
				if(!flag)
					drv_led_off(1);
				else
					drv_led_on(1);
				comm_global_critical_section_leave();
				flag = !flag;
			}
		}
	}
	while(1);
}

/*
 * kernel main function
 */
void head_main(void)
{
	/* initialize all modules */
	ll_tick_count = 0;
	
	// physical memory initialize
	mem_physical_page_mgr_init(K_CONFIG_SDRAM_START_PHYADDR);

	// virtual memory management initialize
	
	// kernel slab management initialize
	mem_slab_initialize();

	// initialize mutex module
	mt_initialize();

	// initialize task module 
	task_module_init();
	
	// initialize global critical section
	comm_global_critical_section_init();

	// timer initialize
	timer_initialize();

	// interrupt initialize
	int_initialize_table();
	
	// now, we start default process.
	head_create_default_task((unsigned int *)kernel_main_default);
	
	// now, we should start our default process
	task_schedule(NULL);
	
	
	while(1)
	{
		// waitting timer start running default process
	}
}
